package compendiumAid.domain
{
	import compendiumAid.domain.vo.ElementVO;
	import compendiumAid.domain.vo.ItemVO;
	import compendiumAid.domain.vo.MonsterVO;
	import compendiumAid.domain.vo.PowerVO;
	import compendiumAid.domain.vo.RitualVO;
	import compendiumAid.domain.vo.TrapVO;
	
	import mx.controls.Alert;

	public class ElementUtil
	{
		public static function elementObjectToElementVO(object:Object):ElementVO
		{
			var elementVO:*;
			
			if (object.classification == null)
			{
				return null;
			}
			
			switch (object.classification)
			{
				case "Monster" 	: 
					elementVO = new MonsterVO;
					if (object.level 			!= null) { elementVO.level 				= object.level				} else { return null; }
					if (object.groupRole 		!= null) { elementVO.groupRole 			= object.groupRole			} else { return null; }
					if (object.combatRole 		!= null) { elementVO.combatRole 		= object.combatRole			} else { return null; }
					break;
					
				case "Item" 	: 
					elementVO = new ItemVO;
					if (object.cost 			!= null) { elementVO.cost 				= object.cost				} else { return null; }
					if (object.level 			!= null) { elementVO.level 				= object.level				} else { return null; }
					if (object.category 		!= null) { elementVO.category 			= object.category			} else { return null; }
					if (object.enhancementValue != null) { elementVO.enhancementValue 	= object.enhancementValue	} else { return null; }
					break;
				
				case "Power" 	: 
					elementVO = new PowerVO;
					if (object.level 			!= null) { elementVO.level 				= object.level				} else { return null; }
					if (object.actionType 		!= null) { elementVO.actionType 		= object.actionType			} else { return null; }
					if (object.className 		!= null) { elementVO.className 			= object.className			} else { return null; }
					break;
				
				case "Trap" 	:
					elementVO = new TrapVO;
					if (object.groupRole 		!= null) { elementVO.groupRole 			= object.groupRole			} else { return null; }
					if (object.type 			!= null) { elementVO.type 				= object.type				} else { return null; }
					if (object.level 			!= null) { elementVO.level 				= object.level				} else { return null; }
					break;
				
				case "Ritul" 	:
					elementVO = new RitualVO;
					if (object.level 			!= null) { elementVO.level				= object.level				} else { return null; }
					if (object.componentCost 	!= null) { elementVO.componentCost 		= object.componentCost		} else { return null; }
					if (object.price 			!= null) { elementVO.price 				= object.price				} else { return null; }
					if (object.keySkill 		!= null) { elementVO.keySkill 			= object.keySkill			} else { return null; }
					break;
				
				default			: elementVO = new ElementVO; break;
			}
			
			if (object.name 					!= null) { elementVO.name 				= object.name; 				} else { return null; }
			if (object.id 						!= null) { elementVO.id 				= object.id; 				} else { return null; }
			if (object.classification 			!= null) { elementVO.classification 	= object.classification; 	} else { return null; }
			
			
			return elementVO;
		}
		
		public static function compendiumObjectToElementVO(object:Object, classification:String):ElementVO
		{
			var elementVO:*;
			
			switch (classification)
			{
				case "Monster" 	: 
					elementVO = new MonsterVO;
					elementVO.level = object.Level;
					elementVO.groupRole = object.GroupRole;
					elementVO.combatRole = object.CombatRole;
					break;
				
				case "Item" 	: 
					elementVO = new ItemVO;
					elementVO.cost = object.Cost;
					elementVO.level = object.Level;
					elementVO.category = object.Category;
					elementVO.enhancementValue = object.EnhancementValue;
					break;
				
				case "Power" 	: 
					elementVO = new PowerVO;
					elementVO.level = object.Level;
					elementVO.actionType = object.ActionType;
					elementVO.className = object.ClassName;
					break;
				
				case "Trap" 	:
					elementVO = new TrapVO;
					elementVO.groupRole = object.GroupRole;
					elementVO.type = object.Type;
					elementVO.level = object.Level;
					break;
				
				case "Ritul" 	:
					elementVO = new RitualVO;
					elementVO.level = object.Level;
					elementVO.componentCost = object.ComponentCost;
					elementVO.price = object.Price;
					elementVO.keySkill = object.KeySkillDescription;
					break;
				
				default			: elementVO = new ElementVO; break;
			}
			
			elementVO.classification = classification;
			elementVO.id = object.ID;
			elementVO.name = object.Name;
			
			return elementVO;
		}
		
		public static function cleanHTMLString(string:String):String
		{
			var returnValue:String = string;
			var array:Array = returnValue.split(/<p(.*?)<\/p/gs);
			var problemString:String = "";
			
			for each (var section:String in array)
			{
				var index:Number = section.indexOf("<p");
				
				if (index >= 0)
				{
					trace("Problem - " + section);
					problemString = section.substr(index, 40);
					index = returnValue.indexOf(problemString);
					var part1:String = returnValue.substring(0, index);
					var part2:String = returnValue.substring(index);
					returnValue = part1 + "</p>" + part2;
				}
			}
			
			returnValue = returnValue.replace(/<(.)><\/\1>/gs, "");
			
			try
			{
				returnValue = XML(returnValue).toString();
			}
			catch (error:Error)
			{
				Alert.show("HTML Parsing error - " + error.message);
			}
			
			returnValue = returnValue.replace(/^(?:.*?)<div id="detail">(.*?)<\/div>(?:.*?)$/s, "      <div id=\"detail\">$1</div>");
			returnValue = returnValue.replace(/<p>(\s*?)Published in(.*?)<\/p>/gs,							"");
			returnValue = returnValue.replace(/<br\/>(\s*?)Revision(.*?)<br\/>(.*?)<br\/>/gs, 				"");
			returnValue = returnValue.replace(/<br\/>(\s*?)Addition(.*?)<br\/>(.*?)<br\/>/gs, 				"");
			returnValue = returnValue.replace(/<br\/>(\s*?)Update(.*?)<br\/>(.*?)<br\/>/gs, 				"");
			returnValue = returnValue.replace(/<p class="flavor">(\s*?)<b>Description<\/b>(.*?)<\/p>/gs, 	"");
			
			return returnValue;
		}
	}
}